package com.hnit.camellia.controller;


import com.hnit.camellia.entity.Address;
import com.hnit.camellia.result.Result;
import com.hnit.camellia.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/address")
public class AddressController {


    @Autowired
    private AddressService addressService;

    /**
     * 获取默认地址
     * @return
     */
    @GetMapping("/default")
    public Result<Address> getDefaultAddress() {
        Address address = addressService.getAddressByDefault();
        return Result.success(address);
    }


    /**
     * 获取地址列表
     * @return
     */
    @GetMapping("/list")
    public Result<List<Address>> getAddressList() {
        List<Address> addressList = addressService.getAddressList();
        return Result.success(addressList);
    }


    @DeleteMapping("delete/{id}")
    public Result deleteAddress(@PathVariable Integer id) {
        addressService.deleteAddress(id);
        return Result.success();

    }

    /**
     * 修改地址
     * @param address
     * @return
     */
    @PostMapping("/update")
    public Result updateAddress(@RequestBody Address address) {
        addressService.updateAddress(address);
        return Result.success();
    }

    /**
     * 新增地址
     * @param address
     * @return
     */
    @PutMapping("add")
    public Result addAddress(@RequestBody Address address) {
        addressService.addAddress(address);
        return Result.success();
    }
}
